#!/usr/bin/env python3

import importlib
from itertools import cycle
from recipes import *
import random
import argparse
from iterlib import map_with_previous


def reload():
    import chromatic_texture_generator
    importlib.reload(chromatic_texture_generator)


octaves = [r" ", r"'", r"''", r"'''"]
octave_to_number = dict(map(lambda t: (t[1], t[0]), enumerate(octaves)))


def choose_octaves(number_of_them):
    os = take(number_of_them, cycle(octaves))
    random.shuffle(os)
    return os


def choose_chromatics(number_of_them):
    notes = take(number_of_them, cycle(scales))
    # scale or scales above?
    return notes


scales = ["c", "cs", "d", "ds", "e", "f", "fs", "g", "gs", "a", "as", "b"]
scale_to_number = dict(map(lambda t: (t[1], t[0]), enumerate(scales)))
size_of_scales = len(scales)


def scale_and_octave_to_number(so):
    return scale_to_number[so[0]] + octave_to_number[so[1]] * size_of_scales


def fix_octave(previous, current):
    if scale_and_octave_to_number(current) - scale_and_octave_to_number(previous) == 1:
        # added this now
        return scale_and_octave_to_number(current) - scale_and_octave_to_number(previous) + 12


def concatenate_notes_and_octaves(amount):
    noters = choose_chromatics(amount)
    number = choose_octaves(amount)
    return map_with_previous(fix_octave, zip(noters, number), None)


def main():
    my_parser = argparse.ArgumentParser()
    my_parser.add_argument("--chroma", action="store", type=int, required=True)
    my_parser.add_argument("--rhythm", action="store", type=int, required=True)
    my_parser.add_argument("--id", action="store", type=int)

    args = my_parser.parse_args()

    amount = args.chroma
    timer = args.rhythm
    result = concatenate_notes_and_octaves(amount)

    print_notes(result, timer)


def print_notes(result, timer):
    res = ["".join(tups) for tups in result]
    timer2 = str(timer)
    print(r'\language "english"')
    print(r'\version "2.19.82"')
    print(r'\include "lit.ily"')

    print("\n")
    print(r"\score")
    print(r"{")
    print("\n")
    print(r"\new PianoStaff <<")
    print(r'\new Staff = "up" {')
    print(r"\time 3/4")
    print("\n")
    print(r'\clef "treble"')

    for n in res:
        if r"'" in n:
            print(r'\change Staff = "up"')
            print(n + timer2)

        elif r"''" in n:
            print(r'\change Staff = "up"')
            print(n + timer2)

        elif r"'''" in n:
            print(n + timer2)

        elif r"," in n:
            print(r'\change Staff = "down"')
            print(n + timer2)

        elif r",," in n:
            print(r'\change Staff = "down"')
            print(n + timer2)

        elif r",,," in n:
            print(r'\change Staff = "down"')
            print(n + timer2)

        else:
            print(r'\change Staff = "down"')
            print(n + timer2)

    print("\n")
    print(r"}")
    print("\n")

    print(r'\new Staff = "down" {')
    print(r"\time 3/4")
    print(r'\clef "bass"')

    for n in res:
        print("s" + timer2)

    print(r"}")
    print(r">>")
    print("\n")
    print(r"\layout { }")
    print(r"\midi { }")
    print(r"}")



if __name__ == "__main__":
    main()
